package com.example.zce.back.common;

import org.springframework.util.Assert;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求代理信息
 */
public class ServletRequestProxy {

    private MultipartResolver multipartResolver = new StandardServletMultipartResolver();

    public ServletRequestProxy() {
        super();
    }

    public ServletRequestProxy(ServletRequestAttributes servletRequestAttributes) {
        Assert.notNull(servletRequestAttributes, "ServletRequestAttributes cannot be empty");
        HttpServletRequest httpServletRequest = servletRequestAttributes.getRequest();
        this.setServletRequest(httpServletRequest);
    }

    private HttpServletRequest servletRequest;

    private boolean multipart = false;

    private MultipartHttpServletRequest multipartRequest;

    public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest httpServletRequest) {
        Assert.notNull(httpServletRequest, "HttpServletRequest cannot be empty");
        if (this.multipartResolver.isMultipart(httpServletRequest)) {
            MultipartHttpServletRequest multipartHttpServletRequest = this.multipartResolver.resolveMultipart(httpServletRequest);
            this.servletRequest = httpServletRequest;
            this.multipartRequest = multipartHttpServletRequest;
            multipart = true;
        } else {
            this.servletRequest = httpServletRequest;
        }
    }

    public MultipartHttpServletRequest getMultipartRequest() {
        return multipartRequest;
    }

    public boolean isMultipart() {
        return multipart;
    }

}
